home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interactive Web Graphics with Shout 3D
/
Interactive Web Graphics With Shout 3D.iso
/
mac
/
Code
/
Chapter Code.exe
/
Chapter09
/
BouncingPanel.java
< prev
next >
Wrap
Text File
|
2000-08-27
|
2KB
|
73 lines
package applets;
import shout3d.*;
import shout3d.core.*;
import shout3d.math.*;
public class BouncingPanel extends Shout3DPanel implements RenderObserver{
Transform t;
float cycleAngle = 0.0f;
float startHeight = 3.0f;
float startCycleTime = 2.0f;
float declineFactor = 1.0f;
boolean bouncing = true;
public BouncingPanel (Shout3DApplet applet){
super(applet);
}
public void customInitialize() {
getRenderer().addRenderObserver(this, null);
t = (Transform) getNodeByName("trans");
}
protected void finalize() {
getRenderer().removeRenderObserver(this);
}
public void onPreRender (Renderer r, Object o) {
if (bouncing) {
//compute decline factor at 8 percent per second
declineFactor = declineFactor - (.08f/getFramesPerSecond());
float cycleTime = startCycleTime * declineFactor;
//use 3.14 because there are
//two complete bouces in a cycle
float deltaAngle = (3.14f/cycleTime)/getFramesPerSecond();
cycleAngle = cycleAngle + deltaAngle;
float height = startHeight * declineFactor;
float yPos = (float) (Math.cos(cycleAngle) * height);
//no negative height
yPos = Math.abs(yPos);
t.translation.set1Value(1, yPos);
//stop bouncing when height is low
if (height < .1) {
bouncing = false;
}
}
}
public void onPostRender (Renderer r, Object o) {
}
} //end of class